package com.dragon.sdgp.util.regexpDecode.handler;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @AUTHOR : Dragon_Yang
 * @File : SplitHandler
 * @DESCRIPTION :
 * @DATE : 2023/3/4 17:23
 */
public class SplitHandler extends BaseDecodeHandler {
    @Override
    public List<MetaData> handle(String exp) {
        System.out.println(exp);
        String[] strings = exp.split("[→←]");
        List<MetaData> metaDatas = Arrays.stream(strings)
                .filter(s -> !"".equals(s))
                .map(s -> {
                    MetaData metaData = new MetaData();
                    String[] arr = s.split("↑");
                    if (arr.length == 1) {
                        metaData.minCount = 1;
                        metaData.maxCount = 1;
                    } else {
                        String[] nums = arr[1].substring(1, arr[1].length() - 1).split(",");
                        if(nums.length == 1){
                            metaData.minCount = Integer.parseInt(nums[0].trim());
                            metaData.maxCount = metaData.minCount;
                        }else{
                            metaData.minCount = Integer.parseInt(nums[0].trim());
                            metaData.maxCount = Integer.parseInt(nums[1].trim());
                        }
                    }
                    StringBuilder stringBuilder = new StringBuilder(arr[0]);
                    int offset = -1;
                    while ((offset = stringBuilder.indexOf("\\", offset + 1)) != -1){
                        char c = stringBuilder.charAt(offset + 1);
                        switch (c){
                            case 'D': {
                                metaData.not = !metaData.not;
                            }
                            case 'd': {
                                metaData.span.add((int) '0');
                                metaData.span.add((int) '9');
                                break;
                            }
                            case 'n': {
                                metaData.option.addLast((int) '\n');
                                break;
                            }
                            case 'S': {
                                metaData.not = !metaData.not;
                            }
                            case 's': {
                                Arrays.asList('\f','\n','\r','\t').stream().forEach(character -> metaData.option.addLast((int) character));
                                break;
                            }
                            default: {
                                metaData.option.addLast((int) c);
                            }
                        }
                        stringBuilder.replace(offset, offset + 2, "");
                    }
                    if(!arr[0].contains("[") || (arr[0].contains("[") && arr[0].contains("\\["))){
                        if(stringBuilder.length() == 0) return metaData;
                        metaData.chars = stringBuilder.toString();  //可能会出bug
                    }else{
                        if(arr[0].contains("^") && !arr[0].contains("\\^")){
                            metaData.not = true;
                            stringBuilder.replace(arr[0].indexOf("^"), arr[0].indexOf("^") + 1, "");
                        }
                        Pattern patternStr = Pattern.compile(".{1}-.{1}");
                        Matcher matcher = patternStr.matcher(stringBuilder.toString());
                        int n = 0;
                        while (matcher.find()) {
                            metaData.span.add((int) stringBuilder.charAt(matcher.start() - (n * 3)));
                            metaData.span.add((int) stringBuilder.charAt(matcher.end() - 1 - (n * 3)));
                            stringBuilder.replace(matcher.start() - (n * 3), matcher.end() - (n * 3), "");
                            n++;
                        }
                        if(stringBuilder.length() > 2) {
                            for (int i = 1; i < stringBuilder.length() - 1; i++) {
                                metaData.span.add((int) stringBuilder.charAt(i));
                                metaData.span.add((int) stringBuilder.charAt(i));
                            }
                        }
                    }

                    return metaData;
        }).collect(Collectors.toList());
        return metaDatas;
    }
}
